<?php

namespace App\Console\Commands;

use App\Jobs\OrderCanceledJob;
use Illuminate\Console\Command;
use App\Internals\BillingInternal;

class OrderCancelCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:order-cancel';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'order cancel';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            $page = 1;
            $pageSize = 100;
            while ($timeoutOrders = BillingInternal::getTimeoutOrder($page, $pageSize)) {
                foreach ($timeoutOrders as $item) {
                    // $userId, $tradeNo, $name, $actualPrice
                    dispatch(new OrderCanceledJob($item['userId'], $item['tradeNo'], $item['name'], $item['actualPrice'], $item['courseLevels']));
                    BillingInternal::cancelOrder($item['id']);
                }
            }
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }

}
